<?php

namespace app\index\controller;

use think\Controller;

class Index extends Controller
{
    public function index()
    {
        $where = [];
        $cateid = input('cateid');
        if ($cateid) {
            $where[] = ['cateid', '=', $cateid];
        }
        $cateInfo = model('Cate')->order('id asc')->select();
        $articleInfo = model('Article')->where($where)->order('id asc')->paginate(5);
        $viewData = [
            'cateInfo' => $cateInfo,
            'articleInfo' => $articleInfo
        ];
        $this->assign($viewData);
        return view();
    }

    public function article()
    {
        $id = input('id');
        $articleInfo = model('Article')->where('id', '=', $id)->find();
        $cateInfo = model('Cate')->order('id asc')->select();
        $viewData = [
            'articleInfo' => $articleInfo,
            'cateInfo' => $cateInfo
        ];
        $this->assign($viewData);
        return view();
    }

    public function loginout()
    {
        session(null);
        if (session('?username')) {
            return $this->error('退出失败！');
        }
        return $this->success('退出成功！', 'index/index/index');
    }

    public function login()
    {
        if (request()->isPost()) {
            $data = input();
            $result = model('User')->login($data);
            if ($result == 1) {
                return $this->success('登录成功！', 'index/index/index');
            }else {
                return $this->error($result);
            }
        }
        $cateInfo = model('Cate')->select();
        $viewData = [
            'cateInfo' => $cateInfo
        ];
        $this->assign($viewData);
        return view();
    }
}
